package com.vmonkey.tbkexport.wechat;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by chiontang on 2/12/16.
 */
public class SnsReader {

	Class SnsDetail = null;
	Class SnsDetailParser = null;
	Class SnsObject = null;
	Parser parser = null;
	ArrayList<SnsInfo> snsList = new ArrayList<SnsInfo>();
	String currentUserId = "";

	public String dbname = "SnsMicroMsg";

	public SnsReader(Class SnsDetail, Class SnsDetailParser, Class SnsObject) {
		this.SnsDetail = SnsDetail;
		this.SnsDetailParser = SnsDetailParser;
		this.SnsObject = SnsObject;
		this.parser = new Parser(SnsDetail, SnsDetailParser, SnsObject);
	}

	public void run() throws Throwable {
		Log.d("wechatmomentstat", "Querying Sns database.");
		queryDatabase();
		Config.isFolderExists(Config.OP_DIR);
		Task.saveToJSONFile(this.snsList, Config.OP_DIR + "/" + dbname
				+ ".json", false);
	}

	public ArrayList<SnsInfo> getSnsList() {
		return this.snsList;
	}

	protected void queryDatabase() throws Throwable {
		String dbPath = Config.DB_DIR + "/" + dbname + ".db";
		if (!new File(dbPath).exists()) {
			Log.e("wechatmomentstat", "DB file not found");
			throw new Exception("DB file not found");
		}
		snsList.clear();
		SQLiteDatabase database = SQLiteDatabase.openDatabase(dbPath, null, 0);
		getCurrentUserIdFromDatabase(database);
		Cursor cursor = database.query("SnsInfo", new String[] { "SnsId",
				"userName", "createTime", "content", "attrBuf", "localFlag",
				"head", "localPrivate", "type", "sourceType", "likeFlag",
				"pravited", "stringSeq" }, "", new String[] {}, "", "",
				"createTime DESC", "");
		while (cursor.moveToNext()) {
			addSnsInfoFromCursor(cursor);
		}
		cursor.close();
		database.close();
	}

	protected void getCurrentUserIdFromDatabase(SQLiteDatabase database)
			throws Throwable {
		Cursor cursor = database.query("snsExtInfo2",
				new String[] { "userName" }, "ROWID=?", new String[] { "1" },
				"", "", "", "1");
		if (cursor.moveToNext()) {
			this.currentUserId = cursor.getString(cursor
					.getColumnIndex("userName"));
		}
		cursor.close();
		Log.d("wechatmomentstat", "Current userID=" + this.currentUserId);
	}

	protected void addSnsInfoFromCursor(Cursor cursor) throws Throwable {
		byte[] snsDetailBin = cursor.getBlob(cursor.getColumnIndex("content"));
		byte[] snsObjectBin = cursor.getBlob(cursor.getColumnIndex("attrBuf"));
		SnsInfo newSns = parser.parseSnsAllFromBin(snsDetailBin, snsObjectBin);

		for (int i = 0; i < snsList.size(); i++) {
			if (snsList.get(i).id.equals(newSns.id)) {
				return;
			}
		}

		if (newSns.authorId.equals(this.currentUserId)) {
			newSns.isCurrentUser = true;
		}

		for (int i = 0; i < newSns.comments.size(); i++) {
			if (newSns.comments.get(i).authorId.equals(this.currentUserId)) {
				newSns.comments.get(i).isCurrentUser = true;
			}
		}

		for (int i = 0; i < newSns.likes.size(); i++) {
			if (newSns.likes.get(i).userId.equals(this.currentUserId)) {
				newSns.likes.get(i).isCurrentUser = true;
			}
		}

		newSns.localFlag = cursor.getString(5);
		newSns.head = cursor.getString(6);
		newSns.localPrivate = cursor.getString(7);
		newSns.type = cursor.getString(8);
		newSns.sourceType = cursor.getString(9);
		newSns.likeFlag = cursor.getString(10);
		newSns.pravited = cursor.getString(11);
		newSns.stringSeq = cursor.getString(12);

		snsList.add(newSns);
		// newSns.print();
	}

}
